import 'package:flutter/material.dart';

void main() {
  // runApp 根组件
  runApp(MaterialApp(
      home: Scaffold(
    appBar: AppBar(
      title: const Text("我的第一个Flutter组件"),
    ),
    body: Column(
      children: [
        const Text('Hello 各位'),
        const StateLessDemo(
          title: "你好世界",
        ),
        const StateFullDemo(),
        Row(
          children: [
            Image.network(
              "https://tse4-mm.cn.bing.net/th/id/OIP-C.Pm1czWZi-tFKDEWWIlE-9AAAAA?w=170&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",
              width: 200,
              height: 200,
              fit: BoxFit.cover,
            ),
            Image.asset(
              "open_eyes.png",
              width: 200,
              height: 200,
              fit: BoxFit.cover,
            )
          ],
        )
      ],
    ),
  )));
}

class StateLessDemo extends StatelessWidget {
  const StateLessDemo({Key? key, this.title}) : super(key: key);

  final String? title;

  @override
  Widget build(BuildContext context) {
    return Text('Hello World${title!}',
        style: const TextStyle(fontSize: 20, color: Colors.red));
  }
}

class StateFullDemo extends StatefulWidget {
  const StateFullDemo({Key? key, this.title}) : super(key: key);

  final String? title;

  @override
  State<StatefulWidget> createState() => _StateFullDemo();
}

class _StateFullDemo extends State<StateFullDemo> {
  int count = 0;
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        ElevatedButton(
          child: const Text('-'),
          onPressed: () {
            // 必须把更新数据放在setState的回调函数中
            setState(() {
              count--;
            });
          },
        ),
        Text('$count'),
        ElevatedButton(
            onPressed: () {
              setState(() {
                count++;
              });
            },
            child: const Text('+'))
      ],
    );
  }
}
